package halberd.filter;

import halberd.DataType;
import halberd.data.Hsv;
import halberd.data.ImgData;
import halberd.data.DImgData;
import halberd.utils.HsvUtils;

public class HsvFilter {
	public ImgData process(ImgData input) {
		if (!canProcess(input)) {
			return null;
		}
		DImgData result = new DImgData(input.getWidth(), input.getHeight());
		rgb2hsv(input.getDatas(), result.getDDatas(), input.getWidth(), input.getHeight());
		result.setDataType(DataType.HSV);
		return result;
	}

	private boolean canProcess(ImgData input) {
		if (input == null) {
			return false;
		}
		DataType dt = input.getDataType();
		if (dt == DataType.RGB) {
			return true;
		}
		if (dt == DataType.RGBA) {
			return true;
		}
		return false;
	}

	private void rgb2hsv(int[][][] src, double[][][] dst, int w, int h) {
		Hsv hsv = new Hsv();
		for (int i = 0; i < w; ++i) {
			for (int j = 0; j < h; ++j) {
				int r = src[i][j][0];
				int g = src[i][j][1];
				int b = src[i][j][2];
				HsvUtils.rgb2hsv(r, g, b, hsv);
				dst[i][j][0] = hsv.h;
				dst[i][j][1] = hsv.s;
				dst[i][j][2] = hsv.v;
			}
		}
	}
}
